package cn.tedu.attendance.service.impl;

import cn.tedu.attendance.mapper.AttendanceMapper;
import cn.tedu.attendance.pojo.entity.Attendance;
import cn.tedu.attendance.pojo.dto.AttendanceSaveParam;
import cn.tedu.attendance.pojo.dto.PageParam;
import cn.tedu.attendance.pojo.vo.PageResult;
import cn.tedu.attendance.service.AttendanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class AttendanceServiceImpl implements AttendanceService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Override
    public List<Attendance> findAll() {
        return attendanceMapper.findAll();
    }

    @Override
    public Attendance findOne(Integer id) {
        return attendanceMapper.findOne(id);
    }

    @Override
    public void add(Attendance attendance) {
        attendanceMapper.add(attendance);
    }

    @Override
    public void update(Attendance attendance) {
        attendanceMapper.update(attendance);
    }


    @Override
    public void delete(Integer id) {
        attendanceMapper.delete(id);
    }

    @Override
    public PageResult<Attendance> pageQuery(PageParam param) {
        Integer count = attendanceMapper.queryCount(param);
        if(count == null || count < 1){
            return new PageResult<>();
        }

        List<Attendance> attendances = attendanceMapper.queryData(param);
        return new PageResult<>(count,attendances);
    }

    @Override
    public void saveAttendance(AttendanceSaveParam attendanceSaveParam) {
        log.debug("保存用户,attendanceSaveParam={}",attendanceSaveParam);
        Attendance attendance = new Attendance();
        BeanUtils.copyProperties(attendanceSaveParam,attendance);
        if(attendance.getId() == null){
            //如果没有id值,新增
            attendanceMapper.add(attendance);
        }else{
            //有id进行修改
            attendanceMapper.update(attendance);
        }
    }
}
